package studentManage;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建集合容器对象
        ArrayList<Student> list = new ArrayList<>();

        ol:while (true){
            //搭建主界面
            System.out.println("-------------------欢迎来到学生管理系统-----------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.退出");
            System.out.println("---------------------请输入您的选项：-------------------");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    addStudent(list);
//                    System.out.println("添加学生");
                    break;
                case "2":
                    delStu(list);
//                    System.out.println("删除学生");
                    break;
                case "3":
                    dateStu(list);
//                    System.out.println("修改学生");
                    break;
                case "4":
//                    System.out.println("查看学生");
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break ol;
                default:
                    System.out.println("您输入的有误");
                    break ;
            }

        }
    }
    //查询学生的方法queryStudent
    public static void queryStudent(ArrayList<Student> list){
        //判断集合中是否有数据
        if(list.size()==0){
            System.out.println("无信息，请添加信息后查询");
        }
        for(int i=0;i<list.size();i++){
            Student s = list.get(i);
            System.out.println("查询信息");
            System.out.println("学号:"+s.getSid());
            System.out.println("姓名:"+s.getName());
            System.out.println("年龄:"+s.getAge()+"岁");
            System.out.println("生日:"+s.getBirthday());
        }
    }

    //添加学生的方法
    public static void addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        //给出要录入的提示信息
        String sid;
        while(true){
            System.out.println("请输入您的学号:");
            sid = sc.next();
            //学号不能重复
            int index = getIndex(list,sid);
            if(index == -1){
                //学号可以使用
                break;
            }
        }
        System.out.println("请输入您的姓名:");
        String name = sc.next();
        System.out.println("请输入您的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入您的生日:");
        String birthday = sc.next();
        //将数据封装到student对象
        Student stu = new Student(sid,name,age,birthday);
        list.add(stu);
        System.out.println("添加成功！");

    }
    //查询学号在集合中的索引
    public static int getIndex(ArrayList<Student> list, String sid) {
        //假设传入的学号不存在,不在集合中
        int index = -1;
        //遍历找到每一个学生对象
        for (int i = 0;i<list.size();i++){
            Student s = list.get(i);
            //获取学生sid
            String id = s.getSid();
            if(id.equals(sid)){
                //存在，让index变量记录正确的索引位置；
                index = i;
            }
        }
        return index;
    }

    public static void delStu(ArrayList<Student>list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学号");
        String sid = sc.next();
        int index = getIndex(list,sid);
        if(index == -1){
            System.out.println("查无此人，请重新输入");
        }else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    public  static  void  dateStu(ArrayList<Student>list){
        System.out.println("请输入你要更改的学生的学号");
        Scanner sc = new Scanner(System.in);
        String dateSid = sc.next();
        int index = getIndex(list,dateSid);
        if(index == -1){
            System.out.println("查无此人，请重新输入");
        }else {
            Student stu =  list.get(index);
            System.out.println("信息如下\n姓名："+stu.getName()+"\n年龄："+stu.getAge()+"\t生日:"+stu.getBirthday());
            System.out.println("请输入您的姓名:");
            String name = sc.next();
            System.out.println("请输入您的年龄:");
            int age = sc.nextInt();
            System.out.println("请输入您的生日:");
            String birthday = sc.next();
            Student  st = new Student(dateSid,name,age,birthday);
            list.set(index,st);
            System.out.println("修改成功");
        }
    }

}
